#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

//insert & erase
void Test_string9(){
  string str = "wo lai le";
  //int pos = 0;
  //while((pos = str.find(' ', pos)) != string::npos)
  //{
  //  str.erase(pos,1);
  //  str.insert(pos,"+");
  //  pos+=4;
  //}

  string retstr;
  for(size_t i = 0; i<str.size(); i++)
  {
    if(str[i]!=' ')
    {
      retstr+=str[i];
    }
    else{
      retstr+='+';
    }
  }

  cout << retstr << endl;
}

//c_str
void Test_string10(){
  string filename = "test.cc";
  FILE *fout = fopen(filename.c_str(), "r");
  char ch = fgetc(fout);
  while(ch != EOF)
  {
    cout << ch;
    ch = fgetc(fout);
  }
  fclose(fout);
}

void Test_string11(){
  string str = "hello";
  str += '\0';
  str += "world";
  cout << "Cstr out: " << str.c_str() << endl;
  cout << "string out: " << str << endl;

  char Ccopy[15];
  string strcopy = str;
  
  strcpy(Ccopy, str.c_str());
  cout << "Cstr copy: " << Ccopy << endl;
  cout << "string copy: " << strcopy << endl;
}

void DealUrl(const string &url);
//find && rfind && substr
void Test_string12(){
  string str = "test.cc.zip";
  //size_t pos = str.find('.');
  size_t pos = str.rfind('.');
  
  if(pos!=string::npos)
  {
    string suff = str.substr(pos);
    cout << suff << endl;
  }

  string url1 = "https://cplusplus.com/reference/string/string/";
  string url2 = "https://blog.csdn.net/zty857016148?spm=1011.2124.3001.5343";
  string url3 = "https://www.baidu.com/";
  string url4 = "https/www.baidu.com/";
  DealUrl(url1);
  cout << endl;
  DealUrl(url2);
  cout << endl;
  DealUrl(url3);
  cout << endl;
  DealUrl(url4);
  cout << endl;
}

void DealUrl(const string &url){
  string protocol;
  string domain;
  string uri;
  size_t pos1 = url.find("://");
  if(pos1 == string::npos)
  {
    cout << "invalid url" << endl;
    return;
  }
  protocol =url.substr(0,pos1);
  
  pos1+=3;
  size_t pos2 = url.find('/', pos1);
  if(pos2 == string::npos)
  {
    cout << "invalid url" << endl;
    return;
  }
  domain = url.substr(pos1, pos2-pos1);
  uri = url.substr(pos2+1);
  cout << "protocol: " << protocol << endl;
  cout << "domain: " << domain << endl;
  cout << "uri: " << uri << endl;
}

//to_string & stoi & stod
void Test_string13(){
  int ival;
  double dval;
  cin >> ival;
  cin >> dval;
  string istr = to_string(ival);
  string dstr = to_string(dval);
  cout << istr << endl;
  cout << dstr << endl;

  istr = "123456";
  dstr = "3.1415";
  ival = stoi(istr);
  dval = stod(dstr);
  cout << ival << endl;
  cout << dval << endl;
}

int main(){
  //Test_string9();
  //Test_string10();
  //Test_string11();
  //Test_string12();
  Test_string13();
  return 0;
}
